package org.example.springvalidation;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.stereotype.Component;


@Component
public class UniqueEmailValidator implements ConstraintValidator<UniqueEmail, UserDTO> {

    @Override
    public void initialize(UniqueEmail constraintAnnotation) {
    }

    @Override
    public boolean isValid(UserDTO value, ConstraintValidatorContext context) {
        if (value == null || value.getEmail() == null || value.getEmail().trim().isEmpty()) {
            //context.disableDefaultConstraintViolation();
            //context.buildConstraintViolationWithTemplate("邮箱不能为空").addPropertyNode("email").addConstraintViolation();
            return false;
        }
        // 模拟检查邮箱是否存在
        if ("test@example.com".equals(value.getEmail())) { // 示例条件
            //context.disableDefaultConstraintViolation();
            //context.buildConstraintViolationWithTemplate("邮箱已被注册").addPropertyNode("email").addConstraintViolation();
            return false;
        }
        return true;
    }
}
